<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Account extends Base_Controller{
	
	private $user;
	private $uid;

	public function __construct(){
		parent::__construct();
		$this->load->library('session');
		$user = $this->session->userdata('annuity_logined');
		if(!$user){
			$this->show_message('无效用户', 301, $this->_url['home'].'/login/');
		}
		$this->user = $user;
		$this->uid = $user['id'];
		$this->smarty_assign([
			'_user' => $user
		]);
		$this->load->model('annuity_user_model');
	}
	
	public function index(){
		$this->load->model('annuity_record_model');
		$nowyear = date('Y');
		$last_month = date('Ym', strtotime('-1 month'));
		
		$records = $this->annuity_record_model->get_result([
			'select' => ['FROM_UNIXTIME(inputtime,"%Y%m") as month', 'amount', 'total'],
			'where' => [
				'uid' => $this->uid,
				'FROM_UNIXTIME(inputtime, "%Y") = ' => $nowyear
			],
			'order_by' => 'month desc'
		]);
		
		$last_amount = $this->annuity_record_model->get_row([
			'select' => ['amount'],
			'where' => [
				'uid' => $this->uid,
				'FROM_UNIXTIME(inputtime, "%Y%m") = ' => date('Ym', strtotime('-1 month'))
			]
		], 'amount');
		
		$year_total = 0;
		foreach($records as $row){
			$year_total += $row['amount'];
		}
		
		$this->ci_smarty->assign([
			'total_amount' => (int)$records[0]['total'],
			'last_amount' => (int)$last_amount,
			'year_total' => (int)$year_total,
			'year_month' => count($records)
		]);
		$this->ci_smarty->display('account_index.htm');
	}
	
	// 个人资料
	public function profile(){
		$info = $this->annuity_user_model->get_row($this->uid);
		$this->ci_smarty->assign([
			'info' => $info
		]);
		$this->ci_smarty->display('account_profile.htm');
	}
	
	// 修改密码
	public function modify_passwd($action = ''){
		if('submit' == $action){
			$posts = $this->input->post();
			if(!$posts){
				$this->output_json();
			}
			$this->load->library('form_validation');
			$this->form_validation->set_rules('old_passwd', '旧密码', 'required');
			$this->form_validation->set_rules('password', '新密码', 'required');
			$this->form_validation->set_rules('confirm_passwd', '确认密码', 'required');
			if($this->form_validation->run() == false){
				foreach($this->form_validation->error_array() as $filed => $error){
					$this->output_json([
						'retcode' => -1,
						'message' => $error
					]);
				}
			}
			$oldpwd = $posts['old_passwd'];
			$password = $posts['password'];
			$confirm = $posts['confirm_passwd'];
			if($password != $confirm){
				$this->output_json('确认密码和新密码不一致');
			}
			$this->load->helper('common');
			$res = $this->annuity_user_model->get_row([
				'id' => $this->uid,
				'password' => mmd5($oldpwd)
			]);
			if(!$res){
				$this->output_json('旧密码不正确');
			}
			$query = $this->annuity_user_model->set([
				'password' => mmd5($password)
			], $this->uid);
			if($query){
				$this->output_json('密码修改成功', 0);
			}
			$this->output_json('密码修改失败');
		}
		$info = $this->annuity_user_model->get_row($this->uid);
		$this->ci_smarty->assign([
			'info' => $info
		]);
		$this->ci_smarty->display('account_password.htm');
	}
	
	// 资金明细
	public function record(){
		$gets = $this->input->get();
		if(!$gets['year']){
			$gets['year'] = date('Y');
		}
		$year = $gets['year'];
		$this->load->model('annuity_record_model');
		
		$total_amount = $this->annuity_user_model->get_row([
			'id' => $this->uid
		], 'total');
		
		$records = $this->annuity_record_model->get_result([
			'uid' => $this->uid,
			'FROM_UNIXTIME(inputtime, "%Y") = ' => $year
		]);
		
		$year_total = 0;
		foreach($records as $row){
			$year_total += $row['amount'];
		}
		
		$this->ci_smarty->assign([
			'gets' => $gets,
			'total_amount' => (int)$total_amount,
			'year_total' => (int)$year_total,
			'records' => $records
		]);
		$this->ci_smarty->display('account_record.htm');
	}
	
}